package com.ruoyi.gen.util;

import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.IdUtil;
import cn.hutool.db.DbUtil;

import java.sql.*;

public class CreateSql {
    public static void main(String[] args) throws SQLException{
        CreateSql.printSql();
    }

    public static final String URL = "jdbc:mysql://192.168.0.80:3306/ry-flowable-plus1";
    public static final String USER = "root";
    public static final String PASSWORD = "root";
    private static Connection conn = null;
    static{
        try {
            //1.加载驱动程序
            Class.forName("com.mysql.cj.jdbc.Driver");
            //2. 获得数据库连接
            conn = DriverManager.getConnection(URL, USER, PASSWORD);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    public static Connection getConnection(){
        return conn;
    }

    public static void printSql() throws SQLException {
        Snowflake snowflake = IdUtil.getSnowflake();
        Connection conn = getConnection();
        Statement stmt = conn.createStatement();
        //根节点
        ResultSet rs = stmt.executeQuery("SELECT * FROM sys_menu where menu_id='4' or menu_id='5'");

        //insert into sys_menu values('5', '办公管理', '0', '5', 'work', null, '', 1, 0, 'M', '0', '0', '', 'job', 'admin', sysdate(), '', null, '办公管理目录');
        //insert into sys_menu values('5', '办公管理', '0', '5', 'work', null, '', 1, 0, 'M', '0', '0', '', 'job', 'admin', sysdate(), '', null, '办公管理目录');

        while(rs.next()){
            String sqlPrefix = "insert into sys_menu values(";
            String sqlInfo = "";
            String id = rs.getString("menu_id");
            Long newId = snowflake.nextId();

            sqlInfo = "'"+newId+"', '"+rs.getString("menu_name")+"', '"+rs.getString("parent_id")+"'" +
                    ", '"+rs.getString("order_num")+"', '"+rs.getString("path")+"', '"+rs.getString("component")+"', " +
                    "'"+rs.getString("query_param")+"', "+rs.getString("is_frame")+", "+rs.getString("is_cache")+", " +
                    "'"+rs.getString("menu_type")+"', '"+rs.getString("visible")+"', '"+rs.getString("status")+"', " +
                    "'"+rs.getString("perms")+"', '"+rs.getString("icon")+"', '"+rs.getString("create_by")+"', " +
                    "sysdate(), '"+rs.getString("update_by")+"', null, '"+rs.getString("remark")+"'";
            String sqlSuffix = ");";
            System.out.println(sqlPrefix + sqlInfo + sqlSuffix);
            printSubSql(id,newId,snowflake);

        }

    }

    public static void printSubSql(String parent_id,Long sub_parent_id,Snowflake snowflake) throws SQLException{
        Connection conn = getConnection();
        Statement stmt = conn.createStatement();
        //根节点
        ResultSet rs = stmt.executeQuery("SELECT * FROM sys_menu where parent_id='"+parent_id+"'");
        while(rs.next()){
            String sqlPrefix = "insert into sys_menu values(";
            String sqlInfo = "";
            String id = rs.getString("menu_id");
            Long newId = snowflake.nextId();
            sqlInfo = "'"+newId+"', '"+rs.getString("menu_name")+"', '"+sub_parent_id+"'" +
                ", '"+rs.getString("order_num")+"', '"+rs.getString("path")+"', '"+rs.getString("component")+"', " +
                "'"+rs.getString("query_param")+"', "+rs.getString("is_frame")+", "+rs.getString("is_cache")+", " +
                "'"+rs.getString("menu_type")+"', '"+rs.getString("visible")+"', '"+rs.getString("status")+"', " +
                "'"+rs.getString("perms")+"', '"+rs.getString("icon")+"', '"+rs.getString("create_by")+"', " +
                "sysdate(), '"+rs.getString("update_by")+"', null, '"+rs.getString("remark")+"'";
            String sqlSuffix = ");";
            System.out.println(sqlPrefix + sqlInfo + sqlSuffix);
            printSubSql(id,newId,snowflake);

        }
    }


}
